package com.monkeygroup.mygoplus.common.utils;

import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * 功能说明:文件夹操作类
 * 作    者:
 * 创建日期:2015/11/4 13:03
 * 所属项目:通用1.0
 */
public class YFileManager {
	private static final String rootName="MYGO";

	/**
	 * 创建文件夹
	 * @throws IOException
	 */
	public static void createDirectory() throws IOException {
		if (hasSDCard() == false) {
			throw new IOException();
		}
		File nf = new File(Environment.getExternalStorageDirectory(), rootName);
		if (nf.exists() == false)
			nf.mkdirs();
		File img = new File(nf, "Images");
		if (img.exists() == false)
			img.mkdirs();

		File db = new File(nf, "Data");
		if (db.exists() == false)
			img.mkdirs();
	}

	/**
	 * 判断是否有SD卡
	 * @return
	 */
	public static boolean hasSDCard() {
		boolean flag = true;
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)  ) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}


	/**
	 * 通过文件名获取路径
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static File getImageFile(String filename) throws IOException {
		createDirectory();
		File root = new File(Environment.getExternalStorageDirectory(),rootName);
		File img = new File(root, "Images");
		File re = new File(img, filename);
		return re;
	}


	/**
	 * 移除图片
	 * @throws IOException
	 */
	public static void removeImageFiles() throws IOException {
		createDirectory();
		File root = new File(Environment.getExternalStorageDirectory(),rootName);
		File img = new File(root, "Images");
		if (img != null && img.exists()) {
			File[] files = img.listFiles();
			if (files != null && files.length > 0) {
				for (File f : files) {
					f.delete();
				}
			}
		}
	}

	public static File getLeaveMsgPicsCacheFile(String filename)
			throws IOException, Exception {

		File root = new File(Environment.getExternalStorageDirectory(), rootName);
		if (root.exists() == false)
		{
			root.mkdirs();
		}

		File useMsgPics = new File(root, "usermsgpics");
		if (useMsgPics.exists() == false)
		{
			useMsgPics.mkdirs();
		}

		return new File(useMsgPics, filename);
	}

}
